import { middleLogger } from '@/utils/back/devlogger';
import { pathsToRegs } from '@/utils/common';
import { NextRequest, NextResponse } from 'next/server';

const nextPaths = [
  '/_next/',
  '/nextasset/',
  '/healthcheck',
  '/redirect',
  '/maintain',
  '/api/(error|login|logout|redirect|configHook|configFile|test)',
  '/categories/',
  '/catalogsearch/',
  '/product/',
  '/new-arrivals($|/)',
  '/promotion($|/)',
  '/compare($|/)',
  '/dictionaries/',
  '/test$',
  '/sitemap.xml',
  '/robots.txt',
];

export default function nextMiddleware(req: NextRequest, next: Fn) {
  const pathname = req.nextUrl.pathname;

  const nextPathRegs = pathsToRegs(nextPaths);
  if (pathname === '/' || nextPathRegs.some(reg => reg.test(pathname))) {
    middleLogger.info('nextMiddleware', pathname);

    return NextResponse.next();
  }

  return next(req);
}
